package dao

import (
	"context"

	"gitee.com/zhaokangming/tiny-ticktok/pkg/constants"
	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	UID     int64  `gorm:"column:uid"`
	VID     int64  `gorm:"column:vid"`
	Content string `gorm:"column:content"`
}

func (m *Comment) TableName() string {
	return constants.CommentTableName
}

func QueryCommentCount(ctx context.Context, vid int64) (int64, error) {
	var count int64
	if err := DB.WithContext(ctx).Model(&Comment{}).Where("vid = ?", vid).Count(&count).Error; err != nil {
		return count, err
	}

	return count, nil
}

func CreateComment(ctx context.Context, uid, vid int64, content string) error {
	return DB.WithContext(ctx).Create(&Comment{UID: uid, VID: vid, Content: content}).Error
}

func DeleteComment(ctx context.Context, uid, cid int64) error {
	return DB.WithContext(ctx).Where("uid = ? and id = ?", uid, cid).Delete(&Comment{}).Error
}

func MGetComments(ctx context.Context, vid int64) ([]Comment, error) {
	var comments []Comment
	if err := DB.WithContext(ctx).Where("vid = ?", vid).Find(&comments).Error; err != nil {
		return nil, err
	}

	return comments, nil
}
